package com.imageim.yimirror.common.ui.tip;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.imageim.yimirror.R;
import com.imageim.yimirror.base.BaseApplication;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2016/3/6.
 */
public class TipUtil {
    private static Toast toast;

    public static void show(final Activity context, final String msg) {
        if (context == null || context.isFinishing()) {
            return;
        }
        context.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

    public static void show(final String msg) {
        if (toast == null) {
            toast = Toast.makeText(BaseApplication.getContext(), msg, Toast.LENGTH_LONG);
        }
        toast.setText(msg);
        toast.show();
    }

    private static Toast bigToast;

    /**
     * 将Toast封装在一个方法中，在其它地方使用时直接输入要弹出的内容即可
     */
    public static void showBigMsg(Context context, String message) {
        if (bigToast == null) {
            View view = View.inflate(context, R.layout.layout_toast, null); //加載layout下的布局
            TextView tvMsg = view.findViewById(R.id.tv_msg);
            if (!TextUtils.isEmpty(message)) {
                tvMsg.setText(message);
            }
            bigToast = new Toast(context);
            bigToast.setGravity(Gravity.CENTER, 0, 0);//setGravity用来设置Toast显示的位置，相当于xml中的android:gravity或android:layout_gravity
            bigToast.setDuration(Toast.LENGTH_LONG);//setDuration方法：设置持续时间，以毫秒为单位。该方法是设置补间动画时间长度的主要方法
            bigToast.setView(view);
        }
        TextView tvMessage = bigToast.getView().findViewById(R.id.tv_msg);
        if (!TextUtils.isEmpty(message)) {
            tvMessage.setText(message);
        }
        bigToast.show();
    }

    /**
     * 自定义Toast时间方法
     * */

    public static void showMyToast(final Toast toast, final int cnt) {
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                bigToast.show();
            }
        },0,5000);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                bigToast.cancel();
                timer.cancel();
            }
        }, cnt );
    }

}
